package com.example.service.impl;

import com.example.common.ServiceResponse;
import com.example.common.TableServerResponse;
import com.example.mapper.PropertyMapper;
import com.example.pojo.BsArea;
import com.example.pojo.BsCity;
import com.example.pojo.BsProvince;
import com.example.pojo.BsStreet;
import com.example.service.IPropertyService;
import com.example.vo.PropertyListVO;
import com.example.vo.UserAllVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: gxcwhd
 * @BelongsPackage: com.example.service.impl
 * @Author: LJ
 * @CreateTime: 2020-08-14 16:52
 * @Description:
 */
@Service
public class PropertyServiceImpl implements IPropertyService {
    @Autowired
    PropertyMapper propertyMapper;

    @Override
    public TableServerResponse getPropertyList(Integer page, Integer limit) {
        //1.开启分页查询
        PageHelper.startPage(page, limit);
        //page = (page - 1)*pageSize;
        //2.执行查询操作
        List<PropertyListVO> list = propertyMapper.getPropertyList();
        //3.取出总共有多少条数据,使用分页实体类来取
        PageInfo pageInfo = new PageInfo(list);
        return TableServerResponse.createSuccess(0, list, pageInfo.getTotal());
    }

    @Override
    public TableServerResponse searchPropery(PropertyListVO propertyListVO) {
        List<PropertyListVO> list = propertyMapper.searchProperty(propertyListVO);
        return TableServerResponse.createSuccess(list);
    }

    @Override
    public ServiceResponse getProvince() {
        List<BsProvince> province = propertyMapper.getProvince();
        return ServiceResponse.createSuccess(province);
    }

    @Override
    public ServiceResponse getCity(String provinceCode) {
        List<BsCity> city = propertyMapper.getCity(provinceCode);
        return ServiceResponse.createSuccess(city);
    }

    @Override
    public ServiceResponse getArea(String cityCode) {
        List<BsArea> area = propertyMapper.getArea(cityCode);
        return ServiceResponse.createSuccess(area);
    }

    @Override
    public ServiceResponse getStreet(String areaCode) {
        List<BsStreet> street = propertyMapper.getStreet(areaCode);
        return ServiceResponse.createSuccess(street);
    }

    @Override
    public ServiceResponse addProperty(PropertyListVO propertyListVO) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        System.out.println(sdf);
        propertyListVO.setCreateTime(format);
        Integer integer = propertyMapper.addProperty(propertyListVO);
        if (integer > 0){
            return ServiceResponse.createSuccess();
        }else {
            return ServiceResponse.createError();
        }
    }

    @Override
    public ServiceResponse updateProperty(PropertyListVO propertyListVO) {
        Integer integer = propertyMapper.updateProperty(propertyListVO);
        if (integer > 0 ){
            return ServiceResponse.createSuccess();
        }else {
            return ServiceResponse.createError();
        }
    }

    @Override
    public ServiceResponse getAllStreet() {
        List<BsStreet> list = propertyMapper.getAllStreet();
        return ServiceResponse.createSuccess(list);
    }
}
